package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.dto.GetStoreNameDto;
import com.wnxy.pawnbackgroundsystem.dto.InsertStoreDto;
import com.wnxy.pawnbackgroundsystem.dto.StoreDto;
import com.wnxy.pawnbackgroundsystem.dto.WareHouseDto;
import com.wnxy.pawnbackgroundsystem.entity.Store;
import com.wnxy.pawnbackgroundsystem.dao.StoreMapper;
import com.wnxy.pawnbackgroundsystem.entity.StoreWarehouse;
import com.wnxy.pawnbackgroundsystem.entity.Warehouse;
import com.wnxy.pawnbackgroundsystem.param.GetStoreParam;
import com.wnxy.pawnbackgroundsystem.param.InsertStoreParam;
import com.wnxy.pawnbackgroundsystem.param.UpdateStoreParam;
import com.wnxy.pawnbackgroundsystem.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 门店管理表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreWarehouseServiceImpl storeWarehouseService;
    @Override
    public void insertStore(InsertStoreParam isp) {
        System.out.println("----------");
        System.out.println(isp);
        Store store = new Store();
        store.setStoreName(isp.getStoreName());
        store.setStorePeople(isp.getStorePeople());
        store.setStorePhone(isp.getStorePhone());
        store.setStoreStatus(isp.getStoreStatus());
        store.setStoreAddress(isp.getStoreAddress());
        save(store);
        List<Integer> name = isp.getName();
        for (Integer integer : name) {
            StoreWarehouse storeWarehouse = new StoreWarehouse();
            storeWarehouse.setWarehouseId(integer+"");
            storeWarehouse.setStoreId(store.getStoreId()+"");
            storeWarehouseService.save(storeWarehouse);
        }


    }



    @Override
    public void delStore(Integer storeId) {

        this.removeById(storeId);
        storeWarehouseService.remove(new QueryWrapper<StoreWarehouse>().eq("store_id", storeId+""));
    }

    @Override
    public List<Store> getStore() {
        List<Store> list = list();
        return list;
    }

    @Override
    public IPage<StoreDto> storeDto(GetStoreParam getStoreParam) {
        HashMap<String, Object> map = new HashMap<>();
        if(null!=getStoreParam.getStoreStatus()){
            map.put("StoreStatus",getStoreParam.getStoreStatus());
        }
        if(null!=getStoreParam.getStorePhone()){
            map.put("StorePhone",getStoreParam.getStorePhone());
        }
        if (null!=getStoreParam.getStorePeople()){
            map.put("StorePeople",getStoreParam.getStorePeople());
        }
        if (null!=getStoreParam.getStoreName()){
            map.put("StoreName",getStoreParam.getStoreName());
        }
        map.put("Index",getStoreParam.getPageParam().getIndexPage());
        map.put("Size",getStoreParam.getPageParam().getSize());
        List<StoreDto> stores = storeMapper.stores(map);
        IPage<StoreDto> storeDtoPage = new Page<StoreDto>();
        storeDtoPage.setRecords(stores);
        List<StoreDto> storeDtos = storeMapper.storesCunt(map);
        storeDtoPage.setTotal(storeDtos.size());
        storeDtoPage.setCurrent(getStoreParam.getPageParam().getIndexPage());
        storeDtoPage.setSize(getStoreParam.getPageParam().getSize());
        return storeDtoPage;
    }

    @Override
    public void updeteStore(UpdateStoreParam usp) {
        if (usp!=null){
            Store store= getOne(new QueryWrapper<Store>().eq("store_id", usp.getStoreId()));
            store.setStoreName(usp.getStoreName());
            store.setStorePeople(usp.getStorePeople());
            store.setStorePhone(usp.getStorePhone());
            store.setStoreStatus(usp.getStoreStatus());
            store.setStoreAddress(usp.getStoreAddress());
            this.saveOrUpdate(store);
            storeWarehouseService.remove(new QueryWrapper<StoreWarehouse>().eq("store_id", store.getStoreId()));
            for (Integer integer : usp.getNum()) {
                StoreWarehouse storeWarehouse = new StoreWarehouse();
                storeWarehouse.setStoreId(store.getStoreId()+"");
                storeWarehouse.setWarehouseId(integer+"");
                storeWarehouseService.save(storeWarehouse);
            }

        }


    }


    @Override
    public List<GetStoreNameDto> getAllHouse() {
        List<GetStoreNameDto> wd=new ArrayList<>();
        List<Store> list = this.list();
        for (Store store : list) {
            GetStoreNameDto getStoreNameDto = new GetStoreNameDto();
            getStoreNameDto.setKey(store.getStoreId());
            getStoreNameDto.setLabel(store.getStoreName());
            wd.add(getStoreNameDto);
        }
        return wd;
    }

    @Override
    public List<String> getWareHouseName(Integer storeId) {
        if(null!=storeId){
            ArrayList<String> name = new ArrayList<>();
            List<StoreWarehouse> storeWarehouses = storeWarehouseService.list(new QueryWrapper<StoreWarehouse>().eq("store_id", storeId+""));
            for (StoreWarehouse storeWarehouse : storeWarehouses) {
                name.add(storeWarehouse.getWarehouseId()+"");
            }
            return name;
        }


        return null;
    }


}
